package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.entity.ApAuthor;
import com.heima.article.feign.ArticleFeign;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.UserEnum;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.user.dto.FollowBehaviorDto;
import com.heima.user.dto.UserRelationDto;
import com.heima.user.entity.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.IApUserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import springfox.documentation.spring.web.json.Json;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-06
 */
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements IApUserFollowService {
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Value("${topic.followBehaviorTopic}")
    private String followBehaviorTopic;


    /**
     * 关注和取消关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
//        获取登录信息  /  构建关注对象
        User user = UserThreadLocalUtil.get();
        ApUserFollow apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(user.getUserId());
        apUserFollow.setUserName(user.getUserName());
//         构建关注目标  作者  author

        ResponseResult<ApAuthor> result = articleFeign.getAuthorById(dto.getAuthorId());
        if (result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor author = result.getData();
//            根据userId和followId 作为关注和取消关注的查询条件
            LambdaQueryWrapper<ApUserFollow> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApUserFollow::getUserId, user.getUserId());
            wrapper.eq(ApUserFollow::getFollowId, author.getUserId());
//            根据状态进行分支操作  0:关注   1：取消关注
            if (dto.getOperation().equals(UserEnum.USER_FOLLOW_OPRATION.getVal())) {
                ApUserFollow one = this.getOne(wrapper);
                if (one == null) {
                    apUserFollow.setFollowId(author.getUserId());
                    apUserFollow.setFollowName(author.getName());
                }
            } else {
                this.remove(wrapper);
            }
//            使用Kafka发送消息 发送FollowBehaviorDto实体类
            FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();
            followBehaviorDto.setFollowId(dto.getAuthorId());
            followBehaviorDto.setEquipmentId(dto.getEquipmentId());
            followBehaviorDto.setUserId(user.getUserId());
            followBehaviorDto.setOperation(dto.getOperation());
            kafkaTemplate.send(followBehaviorTopic, JSON.toJSONString(followBehaviorDto));
            return ResponseResult.okResult();
        }
        return ResponseResult.okResult();
    }
}
